package com.view;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.adpter.ReceConIyrUserListAdapter;
import com.android.ocean.util.common.NetWorkUtil;
import com.android.ocean.util.common.StringUtil;
import com.android.ocean.util.common.TranHelper;
import com.contact.ChatActivity;
import com.contact.WebviewActivity;
import com.db.DBService;
import com.http.NetCallBack;
import com.http.NoReadMsgListTask;
import com.matang.R;
import com.modle.User;
import com.util.AppUtil;
import com.util.UserUtil;

/**
 * 
 * @ClassName:XRecentConnectListView
 * @Description:TODO最近联系人 或最近联系机构
 * @author zhuw
 * @date 2013-8-24 下午7:26:30
 */
public class XRecentConnectListView extends BaseFrameLayout {
	private String tag = "XRecentConnectListView";
	ArrayList<User> dataList = new ArrayList<User>();
	ReceConIyrUserListAdapter adapter;
	ListView listView;
	String type = null;
	User user;

	public XRecentConnectListView(Context context) {
		super(context);
		init(context);
	}

	public XRecentConnectListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public XRecentConnectListView(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	private void init(Context context) {
		this.context = context;
		View view = LayoutInflater.from(context).inflate(
				R.layout.l_merge_listview, this);
		listView = (ListView) view.findViewById(R.id.con_listview);
		loadingView = (View) view.findViewById(R.id.loading_view);
		user = UserUtil.readUser();
			
		
	}

	public void initData(String type) {
		this.type = type;
		flushData();
		qData();
		fillPane();
	}

	private void qData() {
		System.out.println("------------");
		if(null!=user)
		this.dataList.addAll(DBService.selectAllRecent(context, user.id));
		}

	private void flushData() {
		if (!StringUtil.isListNull(dataList)) {
			dataList.clear();
		}
		if (adapter != null) {
			adapter.notifyDataSetChanged();
		}
	}

	private void fillPane() {
		if (StringUtil.isListNull(dataList)) {
//			AppUtil.tsMsg(context, "无最近联系人...");
		} else {

			if (adapter == null) {
				adapter = new ReceConIyrUserListAdapter(dataList, context);
				listView.setAdapter(adapter);
			} else {
				adapter.notifyDataSetChanged();
			}
			listView.setOnItemClickListener(new OnItemClickListener() {

				public void onItemClick(AdapterView<?> arg0, View arg1,
						int pos, long arg3) {
					User user1 = (User) arg0.getAdapter().getItem(pos);
					if ("456".equals(user1.id)) {//系统消息
						String sysMessage = user1.signature;
						user1.msgTotalNum="0";
						DBService.inserOrUpdateUser(context, user.id, user1);
						Intent intent2 = new Intent(context,
								WebviewActivity.class);
						intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
						intent2.putExtra("title", sysMessage);
						intent2.putExtra("text",sysMessage);
						context.startActivity(intent2);
						
						
					}else {
						if (NetWorkUtil.isNetworkConnected(context)) {
							dataList.get(pos).msgTotalNum="0";
							DBService.inserOrUpdateUser(context, user.id, dataList.get(pos));
							fillPane();
							//DBService.delRecentUser(context, user.id, dataList.get(pos).id);
							Bundle b = new Bundle();
							b.putSerializable("user", dataList.get(pos));
							TranHelper.toAct(context, ChatActivity.class, b);
						} else {

							AppUtil.tsMsg(context, "请先连接网络,再进入聊天界面");

						}
					}
				
				}
			});

		}
	}
	
final int DO_Q_DATA=911;
final int TIME_INTERAL=20000;//刷新数据间隔
//刷新聊天数据
Handler handler=new Handler(){

	@Override
	public void handleMessage(Message msg) {
	
		switch( msg.what){
		case DO_Q_DATA:{
			
			startQNetData();
		}
	}
	
	
	
}
	

};
public void startQNetData(){
	Log.d(tag, "startQNetData---------------");
	loadNoReadData();
	handler.removeMessages(DO_Q_DATA);
	handler.sendEmptyMessageDelayed(DO_Q_DATA, TIME_INTERAL);
	
}

public void stopQNetData(){
	Log.d(tag, "stopQNetData---------------");

	handler.removeMessages(DO_Q_DATA);
}

	
	private void loadNoReadData() {
		Log.d(tag, "loadNoReadData---------------");
		if (user != null) {
			new NoReadMsgListTask(callBack).execute(type, user.id);
		}else {
			Toast.makeText(getContext(), "请先登录", 0).show();
		}
		
	}

	NetCallBack callBack = new NetCallBack() {

		@Override
		public void onSuccess(Object... obj) {
			Log.d(tag, "onSuccess---------------");
			ArrayList<User> netNoReadList = null;
			if (obj != null) {
				netNoReadList = (ArrayList<User>) obj[0];
			}
			if (!StringUtil.isListNull(netNoReadList)) {
				Log.d(tag, "netNoReadList.size()---------------"+netNoReadList.size());

				saveToDB(netNoReadList);
			}
			initData(type);
		}

		@Override
		public void onError(Object... obj) {
			Log.d(tag, "onError---------------");

			if (obj != null) {
				Log.d(tag, obj[0].toString());

			}
		}
	};

	private void saveToDB(ArrayList<User> list) {
		DBService.saveUserList(context, user.id, list);
	}

}
